<?php

namespace App\Repository\Impl;

if (!defined('IN_PX'))
    exit;

use App\Repository\Product;

class ProductImpl implements Product {

    //仓储层组件
    private function __Repository($value = 'product') {}

    private function __Inject($db) {}

    public function productType() {
        return $this->db->select('*')
            ->table('#@__@product_fication')
            ->where('`is_show` = 1')
            ->order('`sort`')
            ->findAll();
    }

    public function productStyle() {
        return $this->db->select('*')
            ->table('#@__@style')
            ->where('`is_show` = 1')
            ->order('`sort`')
            ->findAll();
    }

    public function productScene() {
        return $this->db->select('*')
            ->table('#@__@scene')
            ->where('`is_show` = 1')
            ->order('`sort`')
            ->findAll();
    }

    public function findProductCount($id, $style, $scene, $search = 0) {
        $_where = ' 0 = 0 AND p.`is_show` = 1 AND p.`is_status` = 1';
        $_bind = array();
        if ($id != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`product_fication_id`)';
            array_push($_bind, $id);
        }
        if ($style != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`style_id`)';
            array_push($_bind, $style);
        }
        if ($scene != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`scene_id`)';
            array_push($_bind, $scene);
        }
        if ($search !== 0) {
            $_where .= ' AND (p.`title` LIKE \'%' . $search . '%\' OR p.`label` LIKE \'%' . $search . '%\')';
        }
//        $this->db->debug();
        return $this->db->table('#@__@product p')
            ->where($_where)
            ->bind($_bind)
            ->count();
    }

    public function findProductAll($id, $style, $scene, $search = 0, $start = null, $end = null) {
        $_where = ' 0 = 0 AND p.`is_show` = 1 AND p.`is_status` = 1';
        $_bind = array();
        if ($id != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`product_fication_id`)';
            array_push($_bind, $id);
        }
        if ($style != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`style_id`)';
            array_push($_bind, $style);
        }
        if ($scene != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`scene_id`)';
            array_push($_bind, $scene);
        }
        if ($search !== 0) {
            $_where .= ' AND (p.`title` LIKE \'%' . $search . '%\' OR p.`label` LIKE \'%' . $search . '%\')';
        }
//        $this->db->debug();
        return $this->db->select('p.*')
            ->table('#@__@product p')
            ->where($_where)
            ->bind($_bind)
            ->limit($start, $end)
            ->order('p.add_date', 'DESC')
            ->findAll();
    }
    public function findProductRand($id, $style, $scene, $search = 0, $start = null, $end = null) {
        $_where = ' 0 = 0 AND p.`is_show` = 1 AND p.`is_status` = 1';
        $_bind = array();
        if ($id != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`product_fication_id`)';
            array_push($_bind, $id);
        }
        if ($style != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`style_id`)';
            array_push($_bind, $style);
        }
        if ($scene != 0) {
            $_where .= ' AND FIND_IN_SET(?, p.`scene_id`)';
            array_push($_bind, $scene);
        }
        if ($search !== 0) {
            $_where .= ' AND (p.`title` LIKE \'%' . $search . '%\' OR p.`label` LIKE \'%' . $search . '%\')';
        }
        $this->db->debug();
        return $this->db->select('p.*')
            ->table('#@__@product p')
            ->where($_where)
            ->bind($_bind)
            ->limit($start, $end)
            ->order('rand()')
            ->findAll();
    }

    public function findProductDetail($id) {
        return $this->db->select('*')
            ->table('#@__@product')
            ->where('`is_show` = 1 AND `img_sn` = ?')
            ->bind(array($id))
            ->find();
    }


    public function findProductLike($ID) {
        $this->db->debug();
        return $this->db->select('l.`admin_id`, p.*')
            ->table('#@__@like_goods l LEFT JOIN #@__@product p ON l.`like_product_id` = p.`product_id`')
            ->where('l.`product_id` = ?')
            ->bind(array($ID))
            ->findAll();
    }

    //收藏数量
    public function findCollectionCount($id, $memberId) {
        return $this->db->table('#@__@collection_product')
            ->where('`product_id` = ? AND `member_id` = ?')
            ->bind(array($id, $memberId))
            ->count();
    }

    //是否购买
    public function findOrderCount($id, $memberId) {
//        $this->db->debug();
        return $this->db->table('#@__@order')
            ->where('`product_id` = ? AND `member_id` = ? AND `order_status` = 1')
            ->bind(array($id, $memberId))
            ->count();
    }

    public function findOrderDetail($id, $memberId) {
        $this->db->debug();
        return $this->db->select('*')
            ->table('#@__@order')
            ->where('`product_id` = ? AND `member_id` = ? AND `order_status` = 1')
            ->bind(array($id, $memberId))
            ->find();
    }

    public function findRightImg($id) {
        $this->db->debug();
        return $this->db->select('`product_img`, `serialNumber`')
            ->table('#@__@product')
            ->where('`product_id` = ?')
            ->bind(array($id))
            ->find();
    }

    public function productCount($memberId, $productId) {
        return $this->db->table('#@__@order')
            ->where('`member_id` = ? AND `img_sn` = ? AND `order_status` = 1')
            ->bind(array($memberId, $productId))
            ->count();
    }

    public function findUserBalance($memberId) {
        return $this->db->select('`balance`')
            ->table('#@__@member')
            ->where('`member_id` = ?')
            ->bind(array($memberId))
            ->find()->balance;
    }

    
    
}
